package com.eastcom.dataquality.core.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.eastcom.dataquality.core.module.Atom_DataSetStatis;
import com.eastcom.dataquality.core.util.UUIDHexGenerator;

public class DayStatisDao extends JdbcDaoSupport implements RowMapper<Atom_DataSetStatis>{
	private UUIDHexGenerator uuid=new UUIDHexGenerator();
	
	public void insert(List<Atom_DataSetStatis> dayStatis){
		String sql="insert into QA_DATASET_DAYSTATI(ID,DATASETID,DATASETNAME,NETYPE,UNINTEGER_RATIO,DIMENSION_NUM,KPIABNORMAL_NUM,KPIABNORMAL_RATIO,INTEGER_SUM,KPI_SUM,TIME_STAMP,TIMEINTEGER_RATIO) " +
				"values(?,?,?,?,?,?,?,?,?,?,?,?)";
		getJdbcTemplate().batchUpdate(sql, new DayStatisBatch(dayStatis));
	}
	
	public void delete(String dataset_id,Date startDate,Date endDate){
		String sql="delete from QA_DATASET_DAYSTATI where DATASETID=? and TIME_STAMP>=? and TIME_STAMP<?";
		Object[] args=new Object[]{dataset_id,startDate,endDate};
		int[] types=new int[]{Types.VARCHAR,Types.TIMESTAMP,Types.TIMESTAMP};
		getJdbcTemplate().update(sql, args, types);
	}
	
	public List<Atom_DataSetStatis> findAll(){
		String sql="select * from QA_DATASET_DAYSTATI";
		List<Atom_DataSetStatis> results=getJdbcTemplate().query(sql, this);
		return results;
	}
	
	class DayStatisBatch implements BatchPreparedStatementSetter{
		private List toInsert;
		
		public DayStatisBatch(List toInsert){
			this.toInsert=toInsert;
		}
		
		public int getBatchSize() {
			return toInsert.size();
		}

		public void setValues(PreparedStatement ps, int i) throws SQLException {
			Atom_DataSetStatis dayBean=(Atom_DataSetStatis)toInsert.get(i);
			ps.setString(1, uuid.generate().toString());
			ps.setString(2, dayBean.getDataSetId());
			ps.setString(3, dayBean.getDataSetName());
			ps.setString(4, dayBean.getNeType());
			ps.setString(5, dayBean.getUnIntegerRatio());
			ps.setString(6, dayBean.getDimensionNum());
			ps.setString(7, dayBean.getKpiAbnormalNum());
			ps.setString(8, dayBean.getKpiAbnormalRatio());
			ps.setString(9, dayBean.getIntegerSum());
			ps.setString(10, dayBean.getKpiSum());
			ps.setTimestamp(11, new java.sql.Timestamp(dayBean.getTimeStamp().getTime()));
			ps.setString(12, dayBean.getAbsenceRatio());
		}
	}

	public Atom_DataSetStatis mapRow(ResultSet rs, int rowNum)
			throws SQLException {
		Atom_DataSetStatis statis=new Atom_DataSetStatis();
		statis.setDataSetId(rs.getString("DATASETID"));
		statis.setDataSetName(rs.getString("DATASETNAME"));
		statis.setNeType(rs.getString("NETYPE"));
		statis.setUnIntegerRatio("UNINTEGER_RATIO");
		statis.setDimensionNum(rs.getString("DIMENSION_NUM"));
		statis.setKpiAbnormalNum(rs.getString("KPIABNORMAL_NUM"));
		statis.setKpiAbnormalRatio(rs.getString("KPIABNORMAL_RATIO"));
		statis.setIntegerSum(rs.getString("INTEGER_SUM"));
		statis.setKpiSum(rs.getString("KPI_SUM"));
		statis.setTimeStamp(rs.getTimestamp("TIME_STAMP"));
		statis.setAbsenceRatio(rs.getString("TIMEINTEGER_RATIO"));
		return statis;
	}
}
